<?php
/**
 */
//发送消息提示
function set_workerman2($pid){
	cfc('jinyun_socket')->send2room('',$pid);//发送给这个ID
	return true;
}
//接收消息提示
function get_workerman2($setmsg){
	//链接tcp
	$return['sign']=sha1(TIMESTAMP.$setting['cloudkey'].$_SESSION['uniacid'].$_SESSION['uid'].$setting['secret']);
	$return['sockettcp']=$return['sign'].';'.TIMESTAMP.';'.$setting['cloudkey'].';'.$_SESSION['uniacid'].';'.$_SESSION['uid'].';0';
	$return['setmsg']=$setmsg;
	$return['mid']=$_SESSION['uid'];
	return $return;
}
//确认是否未读信息
function checksms2($uid,$type){
	$time= mktime(0,0,0,date('m'),date('d')-3,date('Y'));
	$msg = pdo_getall('jy_jdpm_notice_log', array('uniacid'=>$_SESSION['uniacid'],'createtime >='=>$time,'mid'=>$uid,'isread'=>0,'type'=>$type),array('pid','id','msg','url'),'','createtime asc');
	if(!empty($msg)){
		foreach($msg as $k=>&$item){
			if($item['pid']){
				$endtime =pdo_getcolumn('jy_jdpm_auction',array('id'=>$item['pid']),'endtime');
				if($endtime <time()){
					unset($msg[$k]);
				}
			}
		}
		if(!empty($msg)){
			$msg = array_values($msg);
			if(count($msg)>1){
				$return = $msg[0];
			}else{
				$return = $msg[0];
			}
			pdo_update('jy_jdpm_notice_log', array('isread'=>1),array('mid'=>$uid,'id'=>$msg[0]['id']));
		}else{
			$return = false;
		}
	}else{
		$return = false;
	}
	return $return;
}


/**
 * 手机号码隐藏处理
 * @param string $mobile
 * @return string
 */
function mobileProcessing2($mobile = '') {
    if (!preg_match('/^1\d{10}$/' , $mobile)) {
        return $mobile;
    }
    return substr($mobile , 0 , 3) . '****' . substr($mobile , -4);
}
function plugin_set2($key='setting',$data=false){
		if($data){
			$data=ijson_encode($data);
			$id=pdo_getcolumn('core_account_plugin_setting',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>'jy_jdpm','key'=>$key),'id');
			if($id){
				pdo_update('core_account_plugin_setting',array('data'=>$data),array('id'=>$id));
			}else{
				pdo_insert('core_account_plugin_setting',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>'jy_jdpm','key'=>$key,'data'=>$data));
			}
			return true;
		}else{
			$uniacid=$_SESSION['uniacid']?$_SESSION['uniacid']:1;
			$data=pdo_getcolumn('core_account_plugin_setting',array('uniacid'=>$uniacid,'plugin'=>'jy_jdpm','key'=>$key),'data');
			if($data){
				$data=json_decode($data,true);
			}else{
				$data=array();
			}
			return $data;
		}
}
function set_sms2($mobile,$msg='',$msgtype=0,$uid) {
		$tpls = array('0'=>'alicode_tpl','1'=>'aliisout_tpl','2'=>'alisuccess_tpl','3'=>'aligetmoney_tpl','4'=>'alishenfen_tpl','5'=>'alipay_tpl');
		$msgset = plugin_set2('message');
		$smstpl = $msgset[$tpls[$msgtype]];
		// 每个手机号每分钟只能发送一次
		if($uid){
			$count = pdo_count('jy_jdpm_notice_log',array('mid'=>$uid,'type'=>0,'createtime >'=> TIMESTAMP - 60),'id');
			if ($count) {
				cache_delete('commodity-' . $pid);
				//exi('刚刚已发送短信，请一分钟后再次尝试发送短信！','error');
			}
		}
		// 每个ip地址每天请求10次
		if(getip()){
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$count = pdo_count('jy_jdpm_notice_log',array('ip'=>getip(),'type'=>0,'createtime >'=> $daytime),'id');
			if ($count >10) {
				cache_delete('commodity-' . $pid);
				//exi('发送短信太过频繁！','error');
			}
		}
		$uid = $uid ? $uid:0;
		if($msgtype==0){
			$msg2 = '发送验证码:['.$msg['code'].']';
		}
		if($msgtype==1){
			$msg2 = '您的拍品被抢拍！';
		}
		if($msgtype==2){
			$msg2 = '竞拍成功！';
		}
		if($msgtype==3){
			$msg2 = '获得佣金！';
		}
		if($msgtype==4){
			$msg2 = '您的身份实名验证,已审核通过！';
		}
		if($msgtype==5){
			$msg2 = '恭喜您已成功获拍，请于竞拍结束24小时内付清拍品货款。若逾期未付款，将承担违约责任。如已付款请忽略';
		}
		$data = array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$uid,
				'ip'=>getip(),
				'type'=>0,
				'isread'=>1,
				'msg'=>$msg2,
				'createtime'=>TIMESTAMP
		);
		if($msgtype>0 && $msg2){
			pdo_insert('jy_jdpm_notice_log', $data);
		}
		return cfc('jy_alisms')->sendsms($mobile,$msg,$smstpl);
	}
	//发送网页弹窗短信
function set_websms2($setmsg,$setuid=0,$getuid=1,$url='',$pid=0) {
		$data = array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$getuid,
				'ip'=>getip(),
				'type'=>2,
				'url'=>$url,
				'pid'=>$pid,
				'msg'=>$setmsg,
				'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_jdpm_notice_log', $data);
		//set_workerman2($setmsg,$setuid,$getuid);
		return true;
}
function usercom($uid=0) {
		if($uid <1){
			if($uid==0){
				$uid='平台佣金';
			}elseif($uid== -1){
				$uid='服务费';
			}elseif($uid== -2){
				$uid='挂牌手续费';
			}elseif($uid== -3){
				$uid='违约分配金';
			}
		}else{
			$uid =$uid;
		}
		return $uid;
}
/**
 * 已结束-结束时间段查询条件
 * @param  [str] $gt [条件]
 * @return [array]     [条件数组]
 */
function endbid($gt){
    $nowTime=time();
    switch ($gt) {
        // 今天成交
        case '1':
            $end_where = array(
                // 小于明天就是今天的
                'endtime <'=>strtotime(date("Y-m-d",strtotime("+1 day"))),
				'endtime >'=>strtotime(date("Y-m-d",strtotime("-1 day"))),
            );
            break;
        // 昨天成交
        case '2':
            $end_where = array(
                // 大于前天小于今天就是昨天的
					'endtime <'=>strtotime(date("Y-m-d",strtotime("0 day"))),
					'endtime >'=>strtotime(date("Y-m-d",strtotime("+2 day"))),
            );
            break;
        // 前天成交
        case '3':
            $end_where = array(
                // 大于大前天小于昨天就是昨天的

					'endtime >'=>strtotime(date("Y-m-d",strtotime("-3 day"))),
					'endtime <'=>strtotime(date("Y-m-d",strtotime("-1 day"))),
               
            );
            break;
        // 全部结束
        default:
            $end_where = array(
                'endtime <'=>$nowTime
            );
            break;
    }
    // 有人出过价的
    $end_where['mid >']=0;
    // 判处撤拍的
    $end_where['endstatus'] = 4;
    // 当前价大于等于保留价
    //$end_where['nowprice'] = 'Auction.nowprice>=Auction.price';
    $end_where['hide']=0;
    
    return $end_where;
}
function getChildsId($cate, $pid){
        $arr = array();
        foreach($cate as $v){
            if($v['pid'] == $pid){
                $arr[] = $v['id'];
                $arr = array_merge($arr, getChildsId($cate, $v['id']));
            }
        }
        return $arr;
}
// 获取不对用户显示商品的卖家uid集合
function blackuser($uid){
    // 如果登陆了uid【
    if ($uid) {
        $xidarr = array();
        // 买家屏蔽的卖家uid集合
        $selarr= pdo_getall('jy_jdpm_blacklist',array('mid'=>$uid,'selbuy'=>'sel'),array('xid'));
        if ($selarr) {
            $xidarr = $selarr;
        }
        $buyarr= pdo_getall('jy_jdpm_blacklist',array('xid'=>$uid,'selbuy'=>'sel'),array('mid'));
        if ($buyarr) {
            $xidarr = array_merge($xidarr,$buyarr);
        }
        return $xidarr;
    }else{
        return array();
    }
    
    // 如果登陆了获取屏蔽的卖家uid】
}
/**
     * 添加足迹
     */
function addmember() {
	$isadd=pdo_get('jy_jdpm_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	if($isadd){
		return false;
	}
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','username','nickname','realname','avatar','email','fromuser'));
	$paimember =array(
		'mid' => $_SESSION['uid'],
		'uniacid' => $_SESSION['uniacid'],
		'truename' => $member['realname']?$member['realname']:$member['nickname'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_jdpm_member',$paimember);
	$id=pdo_insertid();
	if($member['fromuser']){
		$credit1=pdo_getcolumn('core_members',array('id'=>$_SESSION['fromuser']),'credit1');
		$set=$this->plugin_setting('basic');
		$add=$set['reg']?$set['reg']:30;
		$credit1=$credit1+$add;
		pdo_insert('jy_jdpm_credit_log',array('mid'=>$_SESSION['fromuser'],'is_add'=>1,'fee'=>$add,'remark'=>'推荐注册送积分','createtime'=>time()));
	}
	return $id;
}
/**
 * 格式化金钱
 * @param $num
 * @return string
 */
function FatMoney2($num = 0) {
    return number_format($num , 2);
}
/**
     * 添加足迹
     */
function addFootMark($uid,$pid) {
	pdo_insert('jy_jdpm_member_footmark', array('uniacid'=>$_SESSION['uniacid'],'mid'  => $uid,'pid'  => $pid,'createtime' => time()));
}
function encryptPwd($data) {
    return md5('' . md5($data));
}
//获取get_goodsinfo
function get_goodsinfo($id){
	$pai=pdo_get('jy_jdpm_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','pid','cid','goodsid','auction_no','certificate_no'));
	$goods=pdo_get('jy_weishop_goods',array('id'=>$pai['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('price','title','image','mid','merchid','images','content'));
	$info = array_merge($pai,$goods);
	return $info;
}
//获取get_member
function get_member($mid){
		$member=pdo_get('core_members',array('id'=>intval($mid)),array('nickname','mobile','avatar','credit1','credit2','pcd','sex','email','address','createtime','username','fromuser'));
		return $member?:array();
}
//获取订单
function get_order($orderid){
		$order=pdo_get('jy_weishop_order',array('id'=>intval($orderid)));
		return $order?:array();
}
//获取地址
function get_address($id){
		$address=pdo_get('core_address',array('id'=>intval($id)));
		return $address?:array();
}
?>
